package com.gossip;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.gossip.manager.*;
/**
 * This object represents the service which is responsible for gossiping with other gossip members.
 */
public class GossipService {
	
	/** A instance variable holding the log level. */
	private int _logLevel = LogLevel.INFO;
	
	private GossipManager _gossipManager;
	private MemberListener _memberListener;
	

	public GossipService(String ipAddress, int logLevel, StartupSettings startupSettings) throws InterruptedException, IOException {
		// Set the logging level.
		_logLevel = logLevel;
		int port = 2000;
		String name = ""; 
		
		String[] meArr = ReadMe();
		if(meArr!=null)
		{
			port = Integer.parseInt(meArr[0]);
			name = meArr[1];
		}
		
		_gossipManager = new GossipManager(name, ipAddress, port, startupSettings);
		_memberListener = new MemberListener(port,_gossipManager.getMyself(),startupSettings.getGossipMembers());
	}
	
	public void start() {
		_gossipManager.start();
		_memberListener.start();
	}
	
	public void shutdown() {
		_gossipManager.shutdown();
	}
	
	public static void error(Object message) {
		//if (_logLevel >= LogLevel.ERROR) printMessage(message, System.err);
		printMessage(message, System.err);
	}
	
	public static void info(Object message) {
		//if (_logLevel >= LogLevel.INFO) printMessage(message, System.out);
		printMessage(message, System.out);
	}
	
	public static void debug(Object message) {
		//if (_logLevel >= LogLevel.DEBUG) printMessage(message, System.out);
		printMessage(message, System.out);
	}
	
	private static void printMessage(Object message, PrintStream out) {
		/**String addressString = "unknown";
		if (_me != null)
			addressString = _me.getAddress();
		out.println("[" + addressString + "][" + new Date().toString() + "] " + message);*/
		out.println("[" + new Date().toString() + "] " + message);
	}
	
	private String[] ReadMe()
	  throws IOException
	{
		 File meFile=new File("me.txt");
		 GossipService.debug("Get me from file" + meFile.getAbsolutePath());
         if(!meFile.exists()||meFile.isDirectory())
           throw new FileNotFoundException();
         BufferedReader br=new BufferedReader(new FileReader(meFile));
         String temp=null;
         temp=br.readLine();
         if(temp!=null)
         {
    	     try 
    	     {
    	    	 JSONObject jsonObject = new JSONObject(temp);
   			     String[] result = new String[2];
   			     result[0]=jsonObject.getString("port");
   			     result[1]=jsonObject.getString("name");
   			     return result;
   		     } 
    	     catch (JSONException e) 
    	     {
   			     e.printStackTrace();
   		     }
        }
         return null;
	}
}
